package com.jyothsna.vendingMachine;
import static org.junit.Assert.*;

import java.lang.reflect.Field;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author Jyothsna
*/
public class VendingMachineItemTest {
        public VendingMachineItem vItem;

        /**
         * @throws java.lang.Exception
         */
        @Before
        public void setUp() throws Exception {
                 
        }

        /**
         * @throws java.lang.Exception
         */
        @After
        public void tearDown() throws Exception {

        }

        /**
         * test to create a new VendingMachineItem properly
         */
        @Test
        public final void testVendingMachineItem() throws Exception {
                // test create a new VendingMachineItem correctly
                vItem= new VendingMachineItem("Coke", 1.0);
                assertNotNull("VendingMachineItem is not created", vItem);
                // check type of the created object
                assertEquals(VendingMachineItem.class, vItem.getClass());
               
                //test if name is correctly assigned
                //find the value of private fields name
                Field name = vItem.getClass().getDeclaredField("name");
                name.setAccessible(true);
                assertEquals("Coke", name.get(vItem));
               
                //test if price is correctly assigned
                //find the value of private fields price
                Field price = vItem.getClass().getDeclaredField("price");
                price.setAccessible(true);
                assertEquals(1.0, price.get(vItem));
        }
        
        /**
         * Test method for {@link VendingMachineItem#VendingMachineItem(java.lang.String, double)}.
         * if price is less than zero, create a new VendingMachineItem should throw VendingMachineException
         */


        @Test(expected=VendingMachineException.class)
        public final void testVendingMachineItemInvalidPrice() {
                // test if price is less than zero
                vItem= new VendingMachineItem("Diet Pepsi", -1.5);
        }       

    /**
         * Test method forgetting the name of the Item getName()}.
         */

        @Test
        public final void testGetName() {
                // test to get the name correctly
                vItem= new VendingMachineItem("Coke", 1.0);
                assertEquals("Coke", vItem.getName());
        }

        /**
         * Test when name of the item is null
         */
        @Test(expected=VendingMachineException.class)
        public final void testNullName() {
                // test if price is less than zero
                vItem= new VendingMachineItem("Coke", -1.0);
        }      

        /**
         * Test method for getting the price of the item using getPrice() method
         */
        @Test
        public final void testGetPrice() {
                // test get the price correctly
                vItem= new VendingMachineItem("Snickers", 1.0);
                assertEquals(1.0, vItem.getPrice(), 0.01);
        }

}

